// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Global = require('global');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        hero:{
            type:cc.Node,
            default:null,
        },
        bullet:{
            type:cc.Prefab,
            default:null,
        },
        bullet1:{
            type:cc.Prefab,
            default:null,
        },
        enemy0:{
            type:cc.Prefab,
            default:null,
        },
        enemy1:{
            type:cc.Prefab,
            default:null,
        },
        enemy2:{
            type:cc.Prefab,
            default:null,
        },
        score:{
            type:cc.RichText,
            default:null,
        },
        background:{
            type:cc.Sprite,
            default:null,
        },
        background1:{
            type:cc.Sprite,
            default:null,
        },
        prop:{
            type:cc.Prefab,
            default:null,
        },
        propObject:null,
        propTime:0,
        bombCount:{
            type:cc.Label,
            default:null,
        },
        prop1:{
            type:cc.Prefab,
            default:null,
        },
        prop1Object:null,
        bulletList:[],
        enemyList:[],
        updateIndex:0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        this.node.on(cc.Node.EventType.TOUCH_D, this.onMouseMove, this);
    },

    start () {
        cc.log('开始游戏');
        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;
        
        Global.score = 0;
    },
    onTouchMove(event){
        var mousePoint = event.getLocation();
        var localPoint = this.node.convertToNodeSpace(mousePoint);
        this.hero.x = localPoint.x - 240;
        this.hero.y = localPoint.y - 400;
    },
    onKeyDown(event){
        switch(event.keyCode){
            case cc.macro.KEY.space:
                this.bomb();
        }
    },
    onDestroy(){

    },
    btn_pauseGame_click(){
        if(cc.director.isPaused()){
            cc.director.resume();
        }else{
            cc.director.pause();
        }
    },
    btn_bomb_click(){
        this.bomb();
    },
    bomb(){
        if(this.bombCount.string>0){
            this.bombCount.string--;
            for (var i = 0; i <= this.enemyList.length - 1; i++) {
                if(this.enemyList[i].parent){
                    this.enemyList[i].getComponent('enemy').bomb();
                }
            }
            this.enemyList = [];
        }
    },
    update (dt) {
        this.updateIndex++;
        //背景
        if(--this.background.node.y<=-800)this.background.node.y=800;
        if(--this.background1.node.y<=-800)this.background1.node.y=800;
        //子弹
        if(this.updateIndex%15==0){
            if(this.propTime>0){
                var bullet = cc.instantiate(this.bullet1);
                bullet.x = this.hero.x-30;
                bullet.y = this.hero.y;
                this.node.addChild(bullet);
                this.bulletList.push(bullet);
                var bullet = cc.instantiate(this.bullet1);
                bullet.x = this.hero.x+30;
                bullet.y = this.hero.y;
                this.node.addChild(bullet);
                this.bulletList.push(bullet);
            }else{
                var bullet = cc.instantiate(this.bullet);
                bullet.x = this.hero.x;
                bullet.y = this.hero.y;
                this.node.addChild(bullet);
                this.bulletList.push(bullet);
            }
        }
        for (var i = 0; i <= this.bulletList.length - 1; i++) {
            this.bulletList[i].y += 10;
        }
        for (var i = 0; i <= this.bulletList.length - 1; i++) {
            if(this.bulletList[i].y>400){
                this.bulletList[i].removeFromParent();
                this.bulletList.splice(i,1);
                break;
            }
        }
        //敌人
        if(this.updateIndex%60==0){
            var rand = Math.random()*100;
            var enemy = null;
            if(rand>90){
                enemy = cc.instantiate(this.enemy2);
                enemy.y = 246;
            }else if(rand>70){
                enemy = cc.instantiate(this.enemy1);
                enemy.y = 88;
            }else{
                enemy = cc.instantiate(this.enemy0);
                enemy.y = 38;
            }
            enemy.y += 400;
            enemy.x = Math.random()*480-240;
            enemy.speed = Math.ceil(Math.random()*3);
            this.node.addChild(enemy);
            this.enemyList.push(enemy);
        }
        for (var i = 0; i <= this.enemyList.length - 1; i++) {
            this.enemyList[i].y -= this.enemyList[i].speed*3;
        }
        for (var i = 0; i <= this.enemyList.length - 1; i++) {
            if(this.enemyList[i].y<(-400-246)){
                this.enemyList[i].removeFromParent();
                this.enemyList.splice(i,1);
                break;
            }
        }
        //道具-双倍子弹
        if(this.updateIndex%60==0&&this.propObject==null&&this.propTime==0){
            var prop = cc.instantiate(this.prop);
            prop.x = Math.random()*480-240;
            prop.y = 700;
            this.node.addChild(prop);
            this.propObject = prop;
        }
        if(this.propObject){
            this.propObject.y-=3;
            if(this.propObject.y<-400){
                this.propObject.removeFromParent();
                this.propObject = null;
            }
        }
        if(this.updateIndex%60==0&&this.propTime>0){
            this.propTime--;
        }
        //道具-炸弹
        if(this.updateIndex%120==0&&this.prop1Object==null&&this.bombCount.string<3){
            var prop = cc.instantiate(this.prop1);
            prop.x = Math.random()*480-240;
            prop.y = 700;
            this.node.addChild(prop);
            this.prop1Object = prop;
        }
        if(this.prop1Object){
            this.prop1Object.y-=3;
            if(this.prop1Object.y<-400){
                this.prop1Object.removeFromParent();
                this.prop1Object = null;
            }
        }
    },
});
